package cn.csq.design.creatation.prototype;


/**
 * @author csq
 * @version 1.0.0
 * @ClassName User.java
 * @Description User对象 实现clone接口
 * @createTime 2022年01月03日 16:42:00
 */
public class User implements Cloneable{

    private String name;

    private int age;

    public User() {
        System.out.println("User对象创建");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }


    //简单的clone方法实现，新创建一个user将之前的User的属性赋值给它
    @Override
    protected User clone() throws CloneNotSupportedException {
        User user = new User();
        user.setAge(this.getAge());
        user.setName(this.getName());
        return user;
    }
}
